# 导入selenium
import time

from selenium import webdriver

DRIVER_PATH = './chromedriver.exe'
BROWSER = webdriver.Chrome(DRIVER_PATH)
TARGET_URL = 'https://www.baidu.com/'
BROWSER.get(TARGET_URL)

content = BROWSER.page_source
# print(content)

# 元素定位
# <selenium.webdriver.remote.webelement.WebElement
# (session="04d5c2fd89aa5fdf0c7bb0b44fada288", element="ee1a6a84-976e-4f44-b1b5-927406fe4211")>
inputEl = BROWSER.find_element_by_id('kw')
print(inputEl)

# 输入文本
inputEl.send_keys('acfun')

# 获取【百度一下】点击搜索
searchBtn = BROWSER.find_element_by_id('su')
searchBtn.click()
time.sleep(2)

# 滑动到底部 需要休息两秒，等待浏览器加载dom完成
JS_BOTTOM = 'document.documentElement.scrollTop=100000'
BROWSER.execute_script(JS_BOTTOM)
time.sleep(2)

# 点击下一页
nextPageBtn = BROWSER.find_element_by_xpath('//a[@class="n"]')
nextPageBtn.click()
time.sleep(2)

# 返回上一页
BROWSER.back()
time.sleep(2)

# 向前一页
BROWSER.forward()
time.sleep(2)

BROWSER.quit()

